﻿'********************************************************************************
'*
'*      マスタ履歴　フォーム
'*
'*      File Name : frmStartCompleteDBCheck.vb
'*      Title     : DBチェック着工/完了履歴画面
'*      機能    　：DBチェック着工/完了履歴
'*
'********************************************************************************
'----------------------------------<<修正履歴>>----------------------------------
'ﾊﾞｰｼﾞｮﾝ   日付       作業者        ｺﾒﾝﾄ
'--------- ---------- ------------ -----------------------------------------------
' V1.0.0   2019/10/01 MJF         新規作成

'********************************************************************************

Imports System.Text

Public Class frmStartCompleteDBCheck
    '>>2018/1/31 佐川 キャリア交換対応
    '実行モード
    '0:表示 1:着工 2:製造完成 3:計上完成
    Private execMode As String = VIEW
    '<<2018/1/31 佐川 キャリア交換対応
    '照合結果設定
    Public Overrides Sub SetPassScore()
        PASS_SCORE = 1
        ReDim m_iScore(PASS_SCORE - 1)
        m_iScore = {1}
    End Sub

    'タブコントロールの設定
    Public Overrides Sub SetTabControl()
        TabPageLot.Enabled = False
        TabPageEqp.Enabled = False
    End Sub
    Overrides Sub selectTab()
        tabBase.SelectedIndex = 2

        Me.UpdateStyles() 'ADD 苗 2018/2/3
    End Sub
    'イベント設定
    Public Overrides Sub SetHandler()
        '画面初期化
        inputDataClear()

        ' ラックNo
        AddHandler btnBarCodeRack.Click, AddressOf btnRackNo_Click
        AddHandler txt1002.KeyPress, AddressOf beforeRackNo_KeyPress
        AddHandler txt1003.KeyPress, AddressOf beforeRackNo_KeyPress
        AddHandler txt1004.KeyPress, AddressOf beforeRackNo_KeyPress
        AddHandler txt1005.KeyPress, AddressOf beforeRackNo_KeyPress
        AddHandler txt1006.KeyPress, AddressOf beforeRackNo_KeyPress

        btnBarCodeRack.PerformClick()


    End Sub

    'Public Overrides Sub SetFocus()
    '    txt1002.Enabled = True
    '    txt1002.Focus()
    'End Sub

    'Lot Noでデータベースからデータを取得する
    Public Overrides Sub GetDataByIcTag()
        Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod.Name + " in")

        'ICタグのデータで画面を設定する
        inputDataClear()
        SetFromTag()
        setGridViewLot()

        If m_szFrameid_comp = 1 And IcTag.iFrameIDUse = 1 Then
            getFrameInfo()
            setErrorInfoOnlyNG()

        Else
            GetDefectCata()
        End If

        If String.IsNullOrEmpty(IcTag.szUID) Then
            IcTag = LastTag
        End If
        '現物型式
        setDetailData()
        For Each dr In dtDetailData.Rows
            'If dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lbl0100.Tag) Then
            '    ' ペースト
            '    If dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 1 Then
            '        txt0103.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
            '        txt0104.Text = dr(DB_WORK_RESULT_PHYSICALNAME_MATERIAL_LOT)
            '        lbl0106.Text = dr(DB_WORK_RESULT_PHYSICALNAME_LIMIT_TIME)
            '        'DEL 苗 2018/1/30 BEGIN 材料１だけの対応
            '        'ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 2 Then
            '        '    txt0203.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
            '        '    txt0204.Text = dr(DB_WORK_RESULT_PHYSICALNAME_MATERIAL_LOT)
            '        '    lbl0206.Text = dr(DB_WORK_RESULT_PHYSICALNAME_LIMIT_TIME)
            '        'DEL 苗 2018/1/30 END 材料１だけの対応
            '    End If
            'ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lbl0200.Tag) Then
            '    ' フレーム
            '    If dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 1 Then
            '        txt0303.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
            '    End If
            '    'ADD 苗 2018/1/30 BEGIN フレームのメーカーロット
            '    ' フレーム
            '    If dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 1 Then
            '        txt0304.Text = dr(DB_WORK_RESULT_PHYSICALNAME_MATERIAL_LOT)
            '    End If
            '    'ADD 苗 2018/1/30 ENDフレームのメーカーロット

            'ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lbl0900.Tag) Then
            '    'ウェハケースNo
            '    txt0903.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
            'Else
            If dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lbl1000.Tag) Then
                    'ラックNo
                    If dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 1 Then
                    txt1002.Text = DBNullTOblank(dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL))
                ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 2 Then
                    txt1003.Text = DBNullTOblank(dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL))
                ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 3 Then
                    txt1004.Text = DBNullTOblank(dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL))
                ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 4 Then
                    txt1005.Text = DBNullTOblank(dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL))
                ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 5 Then
                    txt1006.Text = DBNullTOblank(dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL))
                End If
                'ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lblTitleEmptyChips.Tag) Then
                '    lblEmptyChips.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
                'ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lblTitleThrowChips.Tag) Then
                '    lblThrowChips.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
                'ElseIf dr(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG).Equals(lblTitleExcess.Tag) Then
                '    lblExcess.Text = dr(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL)
            End If
        Next

        '>>2018/2/13 大賀 サンプル数量対応
        'If IcTag.Status = TagProcessStatus.Ready Then
        '    lblSampleText.Text = m_cSamplingDef.getSamplingMessage(IcTag.szProcsCD, AggregatePKG)
        'Else
        '    Dim getData = dtDetailData.AsEnumerable().FirstOrDefault(Function(x) lblSampleTitle.Tag.Equals(x("MNF_CND_TAG")))
        '    If getData IsNot Nothing Then
        '        lblSampleText.Text = getData("ACTUAL_MODEL")
        '    End If
        'End If
        '<<2018/2/13 大賀 サンプル数量対応
        getHeadData()

        With m_dtMat
            If .Rows.Count > 0 Then
                '着完日時
                DisplayDate()
            Else
                If m_dtMat.Columns.Count > 0 Then
                    .Rows.Add()
                Else
                    .Columns.Add("BEGIN_TIME")
                    .Rows.Add()
                End If
            End If
        End With

        ' 着工前の場合はb-Site連携を行う
        If IcTag.Status = TagProcessStatus.Ready Then
            getBsiteData()
        End If
    End Sub

    ''画面データ保存（着工時）
    Public Overrides Function SaveEquipMat(ByRef szBeginTime As DateTime) As Boolean
        '>>2018/1/31 佐川 キャリア交換対応
        execMode = BEGIN
        '<<2018/1/31 佐川 キャリア交換対応
        If saveDetail(BEGIN) < 0 Then
            Return False
        Else
            Return True
        End If
    End Function

    ''画面データ保存（製造完成）
    'Public Overrides Function SaveCompleteInfo(Optional ByVal status As ProcessStatus = ProcessStatus.ManufComplete) As Integer
    '    '>>2018/1/31 佐川 キャリア交換対応
    '    execMode = COMPLETE
    '    '<<2018/1/31 佐川 キャリア交換対応
    '    dtDBUpd.Clear()
    '    Return saveDetail(COMPLETE)
    'End Function

    '画面データ保存（計上完成）
    Public Overrides Function SaveTotalInfo() As Integer
        '>>2018/1/31 佐川 キャリア交換対応
        execMode = TOTAL
        '<<2018/1/31 佐川 キャリア交換対応

        '抜取り検査情報を更新
        'RamInspUpdStatus()

        dtDBUpd.Clear()
        Return saveDetail(TOTAL)
    End Function

    'Private Sub RamInspUpdStatus()
    '    Dim objRamInsp As New clsRandomInspection
    '    If lblFailedCnt.Text = "" OrElse lblFailedCnt.Text = "0" Then
    '        '更新DB-抜取り検査の結果：OK
    '        objRamInsp.updateStatus(lotNo, lblProcessCD.Text, "TRUE")
    '    Else
    '        '更新DB-抜取り検査の結果：NG
    '        objRamInsp.updateStatus(lotNo, lblProcessCD.Text, "FALSE")
    '    End If

    'End Sub
    '工程明細テーブル更新処理
    Private Function saveDetail(ByVal type As String) As Integer
        setLotData()
        Dim rowsCnt As Integer = dtDBUpd.Rows.Count
        Dim addIndex As Integer = 0
        If type <> BEGIN Then
            ' ラックNo
            dtDBUpd.Rows.Add()
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG) = lbl1000.Tag
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG_FLG) = CND_TAG_FLG_ELSE
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 1
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL) = txt1002.Text
            addIndex += 1

            dtDBUpd.Rows.Add()
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG) = lbl1000.Tag
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG_FLG) = CND_TAG_FLG_ELSE
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 2
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL) = txt1003.Text
            addIndex += 1

            dtDBUpd.Rows.Add()
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG) = lbl1000.Tag
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG_FLG) = CND_TAG_FLG_ELSE
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 3
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL) = txt1004.Text
            addIndex += 1

            dtDBUpd.Rows.Add()
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG) = lbl1000.Tag
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG_FLG) = CND_TAG_FLG_ELSE
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 4
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL) = txt1005.Text
            addIndex += 1

            dtDBUpd.Rows.Add()
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG) = lbl1000.Tag
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG_FLG) = CND_TAG_FLG_ELSE
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 5
            dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL) = txt1006.Text
            addIndex += 1

        End If
        '>>2018/2/13 大賀 サンプル数量対応
        'サンプル数量
        'dtDBUpd.Rows.Add()
        'dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG) = lblSampleTitle.Tag
        'dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_MNF_CND_TAG_FLG) = CND_TAG_FLG_ELSE
        'dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_SEQ) = 1
        'dtDBUpd.Rows(rowsCnt + addIndex)(DB_WORK_RESULT_PHYSICALNAME_ACTUAL_MODEL) = lblSampleText.Text
        'addIndex += 1
        '<<2018/2/13 大賀 サンプル数量対応
        Return m_cEquipMat.SaveDetail(lotNo, IcTag.szProcsCD, type,
                                      m_szTable, dtDBUpd, m_dtHeader,
                                      m_cIdohyo.EquipInfo.szEquipNo, serverTime)
    End Function

    Private Sub inputDataClear()
        Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod.Name + " in")

        'LOTタブクリア
        clearActualModel()

        ''開始作業者No
        'txtOptNoStt.Text = String.Empty
        ''終了作業者No
        'txtOptNoEnd.Text = String.Empty

        txt1002.Text = String.Empty
        txt1003.Text = String.Empty
        txt1004.Text = String.Empty
        txt1005.Text = String.Empty
        txt1006.Text = String.Empty

    End Sub

    Public Overrides Sub ConfScreenItem()
        Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod.Name + " in")

        If txtLotNo.TextLength > 0 Then
            If IcTag.Status = TagProcessStatus.Ready Then
                '着工待ち
                ' 共通部　製造完成ボタン
                btnManufComp.Enabled = False
                ' 共通部　計上完成ボタン
                btnTotalComp.Enabled = False

            ElseIf IcTag.Status = TagProcessStatus.Processing Then
                '着工中

                '不良項目入力
                btnErrInput.Enabled = True

                ' 共通部　製造完成ボタン
                btnManufComp.Enabled = False

                ' 共通部　計上完成ボタン
                btnTotalComp.Enabled = True

                '着工キャンセル
                btnCancel.Enabled = True


            ElseIf IcTag.Status = TagProcessStatus.ManufComplete Then
                ' 製造完成

                ' 共通部　製造完成ボタン
                btnManufComp.Enabled = False
                ' 共通部　計上完成ボタン
                btnTotalComp.Enabled = True

            Else
                '不正
                MsgBox(getMsgStr("MSG00025"))
            End If
        End If
    End Sub

    'B-Siteチェック
    Private Function getBsiteData() As Boolean
        'B-Site ViewMachineService
        Dim clientProcCd = GetBsiteClientString()
        Dim outMachine = getViewLotService(clientProcCd)

        '取得できないorエラー応答の場合はNG
        If outMachine.Header Is Nothing OrElse outMachine.Header.Status = STS_ERROR Then

            Return False
        End If

        CheckCollScore()
        Return True
    End Function

    '計上完成正常時の画面初期化
    Protected Overrides Sub initializForm()
        MyBase.initializForm()
        inputDataClear()
    End Sub

    '受入MSラックNo入力処理
    Private Sub beforeRackNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        '頭に付いている'%'を消す
        Static Dim fIsPercentInput As Boolean = False
        If e.KeyChar = CChar("%") OrElse e.KeyChar = CChar("％") Then
            fIsPercentInput = True
            e.Handled = True
            Exit Sub
        End If
        If fIsPercentInput Then
            fIsPercentInput = False
            e.Handled = True
            Exit Sub
        End If

        '改行コードが入力された場合は、受入MSラックNoのチェックを行う
        If e.KeyChar = vbCr Then
            Dim txtData As TextBox = CType(sender, TextBox)
            txtData.Text = txtData.Text.ToUpper 'Add 苗 2018/4/10

            If AutoManualMode = 0 And AUTO_MANUAL_BTN = TYPE_AUTO Then

                'Add 苗 2019/11/24 Begin
                Dim tmpLotNo As String
                tmpLotNo = getLotNoByRack(txtData.Text)
                If txtLotNo.Text = "" Or (tmpLotNo <> "" And txtLotNo.Text <> tmpLotNo) Then
                    txtLotNo.Text = tmpLotNo
                    If txtLotNo.Text.Trim() <> "" Then
                        txtLotNo_KeyPress(txtLotNo, New KeyPressEventArgs(vbCr))
                        'If AutoManualMode = 0 Then
                        setRackNo()
                        'End If
                        'Exit Sub
                    End If
                End If
                'Add 苗 2019/11/24 End
            End If
        End If
    End Sub
    Private Sub setRackNo()
        If IsNothing(IcTag.szLotNo) = False Then
            If IcTag.container1 <> "" Then
                txt1002.Text = IcTag.container1
            End If
            If IcTag.container2 <> "" Then
                txt1003.Text = IcTag.container2
            End If
            If IcTag.container3 <> "" Then
                txt1004.Text = IcTag.container3
            End If
            If IcTag.container4 <> "" Then
                txt1005.Text = IcTag.container4
            End If
            If IcTag.container5 <> "" Then
                txt1006.Text = IcTag.container5
            End If

        End If
    End Sub
    ' ラックNo用バーコードクリック
    Private Overloads Sub btnRackNo_Click(ByVal sender As Object, ByVal e As EventArgs)


        rackCount = 0 'ADD MIAO 2018/9/26 

        Dim btn As Button = CType(sender, Button)

        ' 画面の初期化
        For i = 2 To 6 ' ラックNoの入力は02～06。
            Dim textData As TextBox = btn.Parent.Controls.Find(String.Format("txt{0}{1:00}", btn.Tag.ToString, i), False)(0)
            Dim labelData As Label = textData.Parent.Controls.Find(String.Format("lbl{0}{1:00}", textData.Tag.ToString, i), False)(0)
            'textData.Text = String.Empty
            textData.BackColor = TENKEY_INIT_COLOR
            labelData.BackColor = TENKEY_INIT_COLOR
        Next

        ' 入力フォーカスの設定
        Dim txtData As TextBox = btn.Parent.Controls.Find(String.Format("txt{0}02", btn.Tag.ToString), False)(0)
        Dim lblData As Label = txtData.Parent.Controls.Find(String.Format("lbl{0}02", txtData.Tag.ToString), False)(0)
        txtData.Enabled = True
        txtData.Focus()
        txtData.SelectAll()
        txtData.BackColor = Color.Cyan
        lblData.BackColor = Color.Cyan
    End Sub
    Protected Overrides Function isCheckProc() As Boolean
        Return True
    End Function
End Class

